{% extends "base.html" %}

{% block title %}鲍鱼年龄预测{% endblock %}

{% block content %}
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <h2 class="text-primary mb-4">鲍鱼年龄预测</h2>
            
            <!-- 预测表单 -->
            <div class="card mb-4">
                <div class="card-header bg-light">
                    <h5 class="mb-0">预测条件</h5>
                </div>
                <div class="card-body">
                    <form id="predictForm">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="abalone_id" class="form-label">鲍鱼编号</label>
                                    <input type="number"
                                           class="form-control"
                                           id="abalone_id"
                                           name="abalone_id"
                                           placeholder="请输入鲍鱼编号"
                                           min="1"
                                           required>
                                </div>
                            </div>

                            <div class="col-md-6 d-flex align-items-end">
                                <button type="submit" class="btn btn-warning w-100">开始预测</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

            <!-- 批量预测区域 -->
            <div class="card mb-4">
                <div class="card-header bg-info text-white">
                    <h5 class="mb-0">批量预测</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-4">
                            <select class="form-select" id="batch_model_type">
                                <option value="LR">线性回归 (LR)</option>
                                <option value="DT-R">决策树回归 (DT-R)</option>
                                <option value="RF-R">随机森林回归 (RF-R)</option>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <button id="batchPredictBtn" class="btn btn-primary w-100">批量预测</button>
                        </div>
                        <div class="col-md-4">
                            <button id="predictAllBtn" class="btn btn-success w-100">全部模型预测</button>
                        </div>
                    </div>
                    <div id="batchResult" class="mt-3"></div>
                </div>
            </div>

            <!-- 预测结果展示 -->
            <div id="predictionResult" class="card" style="display: none;">
                <div class="card-header bg-success text-white">
                    <h5 class="mb-0">预测结果</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-4">
                            <h4>鲍鱼 <span id="result_id"></span> 号</h4>
                        </div>
                        <div class="col-md-8">
                            <div class="row">
                                <div class="col-md-4">
                                    <div class="alert alert-primary">
                                        <h5 class="alert-heading">线性回归</h5>
                                        <p class="display-6" id="result_LR"></p>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="alert alert-info">
                                        <h5 class="alert-heading">决策树回归</h5>
                                        <p class="display-6" id="result_DT_R"></p>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="alert alert-warning">
                                        <h5 class="alert-heading">随机森林回归</h5>
                                        <p class="display-6" id="result_RF_R"></p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- JavaScript处理 -->
<script>
document.getElementById('predictForm').addEventListener('submit', function(e) {
    e.preventDefault();

    const formData = new FormData(this);
    const resultDiv = document.getElementById('predictionResult');
    resultDiv.style.display = 'none';

    fetch('/predict/', {
        method: 'POST',
        body: formData
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            document.getElementById('result_id').textContent = data.abalone_id;
            document.getElementById('result_LR').textContent = data.predictions.LR;
            document.getElementById('result_DT_R').textContent = data.predictions['DT-R'];
            document.getElementById('result_RF_R').textContent = data.predictions['RF-R'];
            document.getElementById('predictionResult').style.display = 'block';
        } else {
            alert('预测失败: ' + (data.message || '未知错误'));
        }
    })
    .catch(error => {
        console.error('Error:', error);
        alert('请求失败');
    });
});

document.getElementById('batchPredictBtn').addEventListener('click', function() {
    const modelType = document.getElementById('batch_model_type').value;
    const resultDiv = document.getElementById('batchResult');
    resultDiv.innerHTML = '<div class="alert alert-info">批量预测中...</div>';

    fetch('/predict/batch', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        body: `model_type=${modelType}`
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            resultDiv.innerHTML = `
                <div class="alert alert-success">
                    ${data.message}<br>
                    <small>预测数量: ${data.count}</small>
                </div>
            `;
        } else {
            resultDiv.innerHTML = `
                <div class="alert alert-danger">
                    批量预测失败: ${data.message}
                </div>
            `;
        }
    })
    .catch(error => {
        console.error('Error:', error);
        resultDiv.innerHTML = `
            <div class="alert alert-danger">
                请求失败
            </div>
        `;
    });
});

document.getElementById('predictAllBtn').addEventListener('click', function() {
    const resultDiv = document.getElementById('batchResult');
    resultDiv.innerHTML = '<div class="alert alert-info">正在使用所有模型进行预测...</div>';

    fetch('/predict/predict_all_models', {
        method: 'POST'
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            resultDiv.innerHTML = `
                <div class="alert alert-success">
                    ${data.message}<br>
                    <small>预测数量: ${data.count}</small>
                </div>
            `;
        } else {
            resultDiv.innerHTML = `
                <div class="alert alert-danger">
                    预测失败: ${data.message}
                </div>
            `;
        }
    })
    .catch(error => {
        console.error('Error:', error);
        resultDiv.innerHTML = `
            <div class="alert alert-danger">
                请求失败
            </div>
        `;
    });
});
</script>
{% endblock %}